package com.yxw.yx_hotel.controller;

import com.alibaba.fastjson.JSONObject;
import com.yxw.yx_hotel.dto.FileDeleteDto;
import com.yxw.yx_hotel.service.FileService;
import com.yxw.yx_hotel.vo.YxFileVo;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import java.util.List;

/**
 * @author liuxin
 * 2024.01.18
 * 文件管理
 */
@RestController
@RequestMapping("api/file")
@Api(value = "文件管理", tags = "文件管理")
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping(value = "/upload_file")
    @ApiOperation(value = "上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form",name = "multipartFiles", value = "文件流结集合", required = true, dataType = "__file",allowMultiple = true),
            @ApiImplicitParam(name = "bussId", value = "业务ID", required = true),
            @ApiImplicitParam(name = "remark", value = "业务类型：1-客户 2-房间  3-费用 4-入住", required = true),
            @ApiImplicitParam(name = "classify", value = "文件类型：1-视频/音频 2：图片/文件", required = true)
    })
    public BaseResult<List<YxFileVo>> uploadFile(@RequestParam("multipartFiles") MultipartFile[] multipartFiles,
                                                 @RequestParam("bussId") Long bussId,
                                                 @RequestParam("classify") Integer classify,
                                                 @RequestParam("remark") String remark){
        List<YxFileVo> fileList=fileService.uploadFile(multipartFiles,bussId,classify,remark);
        return BaseResult.doSuccess(fileList,"上传成功");
    }

    @DeleteMapping(value = "/deleteFile")
    @ApiOperation(value = "文件删除")
    public BaseResult<Boolean> deleteFile(@RequestBody FileDeleteDto dto) {
        fileService.deleteFile(dto);
        return BaseResult.doSuccess(null,"删除成功");
    }

    @ApiOperation(value = "文件下载/预览")
    @GetMapping(value = "/downloadFile")
    public BaseResult<String> downloadFile(@RequestParam Long fileId){
        String path = fileService.downloadFile(fileId);
        return BaseResult.doSuccess(path,"下载成功");
    }

    @ApiOperation(value = "获取对应业务ID的文件信息列表")
    @PostMapping(value = "/getFileListByBussId")
    public BaseResult<List<YxFileVo>> getFileListByBussId(@RequestParam("bussId") Long bussId){
        List<YxFileVo> list = fileService.getFileListByBussId(bussId);
        return BaseResult.doSuccess(list,"查询成功");
    }
    
    @ApiOperation(value = "根据Url获取对应的文件信息列表")
    @PostMapping(value = "/getFileByUrl")
    public BaseResult getFileByUrl(@RequestParam("fileUrl") String fileUrl){       
        return fileService.getFileByUrl(fileUrl);
    }
    
    @PostMapping(value = "/deleteFileByUrl")
    @ApiOperation(value = "文件删除")
    public BaseResult<Boolean> deleteFileByUrl(@RequestParam("fileUrl") String fileUrl) {
        fileService.deleteFileByUrl(fileUrl);
        return BaseResult.doSuccess(null,"删除成功");
    }
    @ApiOperation(value = "获取一个19个字符新的Long型的Id")
    @GetMapping("/getNewLongId")
    public BaseResult getNewLongId()  {
        JSONObject oJSONObject = new JSONObject();
        oJSONObject.put("id", IdWorker.getId());
        return BaseResult.doSuccess(oJSONObject,"获取成功");
    }
    @ApiOperation(value = "获取一个19个字符新的String型的Id")
    @GetMapping("/getNewStringId")
    public BaseResult getNewStringId()  {
        JSONObject oJSONObject = new JSONObject();
        oJSONObject.put("id", IdWorker.getIdStr());
        return BaseResult.doSuccess(oJSONObject,"获取成功");
    }

    @ApiOperation(value = "获取一个32个字符新的String型的Id")
    @GetMapping("/getUuId")
    public BaseResult getUuId()  {
        JSONObject oJSONObject = new JSONObject();
        oJSONObject.put("id", IdWorker.get32UUID());
        return BaseResult.doSuccess(oJSONObject,"获取成功");
    }      
}
